#!/usr/bin/env python3

import csv
import requests
from time import sleep
from collections import defaultdict
from operator import itemgetter
from collections import Counter
import os.path
from requests.adapters import HTTPAdapter
from pprint import pprint

session= requests.Session()
adapter = HTTPAdapter(max_retries=6)
session.mount('http://', adapter)
session.mount('https://', adapter)


def key_next():
  key = [
      "87b3f94533c3671a5d727e2742b1a0fc",
      "a54d65a9799e7544cb244c9c29c21414",
      "7587265b702434b3f56592c4d1781887",
      "aa9fd403e03ab951c565090f2de20582",
      "ae3bf894484b4b6a318b86d26d3457db",
      "2f2c2af4ccf0ea6ba3533a64c4ce1ea3",
      "f13c0ea5264b21baaf783635f2cf7786",
      "c49b335a7352eae6f86056ff9f6c8a71",
      "60f1555fc0c5b7c995d4b7c699a0c23b",
      "38eabe90d42560c2201a9ee55e00820e",
      "a07dea5f4c21f27f553570edd3c1de07"
      ]
  for i in key:
    yield i

KEY_NEXT = key_next()
EXIST = set()
KEY = KEY_NEXT.__next__()
#url = "http://api.map.baidu.com/place/v2/search"
def search(city, keywords):
  global KEY
  url = "https://restapi.amap.com/v3/place/text"
  offset = 20
  page = 1
  li = []
  while 1:
    r = session.get(
        url,
        params=dict(
          key=KEY,
          keywords=keywords,
          city=city,
          page=page,
          offset=offset,
          extensions="all",
          children=1,
          citylimit="false",
          # query="名品仓",
          # region=city,
          # output="json",
          # ak="wSmlLicE6NgPF7HSVWkNBYML45wkCSrZ",
          # page_num=0,
          # page_size=20,
          ),
        timeout=30
        ).json()
    # if r['status'] == 401:
      #     sleep(1)
      #     continue

    if r['status']=='0' and r['infocode'] == '10003':
      KEY = KEY_NEXT.__next__()
      continue
    if 'count' not in r:
      print(r)
      continue
    count = int(r['count'])
    for i in r['pois']:
      location = i['location']
      if location in EXIST:
        continue
      # pprint(i)
      EXIST.add(location)
      name = i['name']
      li.append([name, i['cityname'], i.get('address','') or '']+location.split(','))
      print(li[-1])
    if offset*page > count:
      break
    page+=1
  return li

#

def find(keywords, 精确搜索=False, func=None):
  keywords = keywords.lower()
  fileprefix = "data/"+keywords+'.'
  csvpath = fileprefix+'csv'
  if os.path.exists(csvpath):
    return
  city_shop = []
  count = Counter()
  with open('city.txt', 'r', encoding="utf-8-sig") as f:
    for i in csv.reader(f):
      city = i[1]
      print(">>",city)
      li = search(city, keywords)
      len_li = len(li)
      if len_li:
        count[city] += len_li
        for j in li:
          if 精确搜索:
            place = j[0].lower().replace("·","")
            if func:
              place = func(place)
            if keywords not in place:
              continue
          city_shop.append(j)
          # break
  # raise
  li = sorted(count.items(),key=itemgetter(1), reverse=True)

  with open(fileprefix+"city.csv", mode='w', encoding="utf-8-sig") as out:
    c = csv.writer(out)
    for i in li:
      c.writerow(i)

  city_shop.sort(key=lambda x:x[1]+" "+x[2])
  with open(csvpath, mode='w', encoding="utf-8-sig") as out:
    c = csv.writer(out)
    for i in city_shop:
      c.writerow(i)


def main():
  li = "沙拉 炸鸡 鲜奶".split(" ")
  for i in li:
    find(i)
    # find('7天连锁', True, lambda x:x.replace('七天','7天'))
    # find('7天优品', True, lambda x:x.replace('七天','7天'))
main()


